﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class WorkAreaDAL : IWorkAreaDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public WorkAreaDAL()
        {

        }
        public IList<WorkAreaInfo> ListWorkAreas(Nullable<int> Seq)
        {
            string strSQL;

            List<WorkAreaInfo> infoList = new List<WorkAreaInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", Seq);

                strSQL = " SELECT WORK_AREA_SEQ, WORK_AREA_ID, WORK_AREA_NAME, SCOPE_SEQ"
                        + ", CREATED_BY, UPDATED_BY"
                      + " FROM HRM_MP_WORK_AREAS"
                      + " WHERE RECORD_STATUS not like @DeleteRecord"
                      + (Seq.HasValue ? " AND WORK_AREA_SEQ = @Seq" : "")
                      + " ORDER BY WORK_AREA_ID";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    WorkAreaInfo Info = new WorkAreaInfo(rdr.GetInt32(0)
                                , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                                , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                                , rdr.IsDBNull(3) ? "" : rdr.GetInt32(3).ToString()
                                , rdr.IsDBNull(4) ? null : (Nullable<int>) rdr.GetInt32(4)
                                , rdr.IsDBNull(5) ? null : (Nullable<int>)rdr.GetInt32(5));
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }      
        
    }
}
